﻿
Namespace Logic.Contracts
    Public Class cContractRGM
        Inherits cContractBase

#Region "Constractor"
        Sub New(ByVal ContractType As String,
                ByVal ContractID As String,
                ByVal ContractBalance As Double?,
                ByVal ContractCurrency As String,
                ByVal ContractIntresetRate As Double?,
                ByVal ContractMaturityDate As Date?,
                ByVal ContractReportDate As Date,
                ByVal ContractMajorGroup As String,
                ByVal ContractProductCode As String,
                Optional ByVal ProductDiscription As String = "",
                Optional ByVal ProductTag As String = "",
                Optional ByVal ProductResponsableGL As String = "")


            MyBase.New()
            Me.ContractID = ContractID
            Me.ContractType = ContractType
            Me.Currency = ContractCurrency
            Me.Balance = ContractBalance
            Me.InterestRate = ContractIntresetRate
            Me.MaturityDate = ContractMaturityDate
            Me.ReportDate = ContractReportDate
            Me.ContractMajorGroup = ContractMajorGroup
            Me.ContractProductCode = ContractProductCode
            Me.ContractProductDiscription = ProductDiscription
            Me.ContractTag = ProductTag
            Me.ResponsableGL = ProductResponsableGL


            Me.CalculateRemainingSeltelemntsNumber()
        End Sub
#End Region

#Region "Methods"
        Public Overrides Function CalculatePaymnetSchedaul() As System.Collections.Generic.List(Of MdulLogic.PaymentSchedual)

            If _CalculateRemainingSeltelemntsNumber = 0 Then
                Dim Pymt As PaymentSchedual
                With Pymt
                    .dblInterestPayment = 0
                    .dblPaymentAmount = Balance
                    .dblPrincipalPayment = Balance
                    .intPaymentDays = 0
                    .strCurrency = Currency
                    .intPaymentOrder = 0
                    .dtePaymentDate = MaturityDate
                    .strTenor = "'" & GetTenor(.intPaymentDays) & "'"
                End With
                _PaymentSchedual.Clear()
                _PaymentSchedual.Add(Pymt)


            Else ' Payment is more than 1 payment or the contract is not mature yet
                For intLooper As Integer = 1 To _CalculateRemainingSeltelemntsNumber
                    Dim pymt As PaymentSchedual
                    Dim PymtDate As Date = _CalculateCurrentSetelmentDate.AddMonths(intLooper - 1)
                    With pymt
                        .strCurrency = Currency
                        .intPaymentOrder = intLooper
                        .intPaymentDays = DateDiff("d", ReportDate, PymtDate)
                        .dblPrincipalPayment = Balance / _CalculateRemainingSeltelemntsNumber
                        Dim dblStartBalance As Double = Balance - (.dblPrincipalPayment * (intLooper - 1))
                        .dblInterestPayment = (dblStartBalance * InterestRate) / 12
                        .dblPaymentAmount = .dblInterestPayment + .dblPrincipalPayment
                        .dtePaymentDate = PymtDate
                        .strTenor = "'" & GetTenor(.intPaymentDays) & "'"
                    End With
                    _PaymentSchedual.Add(pymt)
                Next
            End If
            PaymentAdjestment()
            Return _PaymentSchedual
        End Function

        Public Overrides Function CalculateRemainingSeltelemntsNumber() As Integer
            Dim TestDate As Date = MaturityDate
            _CalculateRemainingSeltelemntsNumber = 0

            If MaturityDate = ReportDate Then
                _CalculateRemainingSeltelemntsNumber += 1
                _CalculateCurrentSetelmentDate = MaturityDate
            End If

            If MaturityDate > ReportDate Then
                If CDate(MaturityDate).Day >= CDate(ReportDate).Day Then
                    _CalculateRemainingSeltelemntsNumber = 0
                    Do Until (TestDate <= ReportDate)
                        _CalculateRemainingSeltelemntsNumber += 1
                        _CalculateCurrentSetelmentDate = TestDate
                        TestDate = TestDate.AddMonths(-1)
                    Loop
                Else
                    _CalculateRemainingSeltelemntsNumber = 0
                    Do Until (TestDate <= ReportDate)
                        _CalculateRemainingSeltelemntsNumber += 1
                        _CalculateCurrentSetelmentDate = TestDate
                        TestDate = TestDate.AddMonths(-1)
                    Loop
                End If

            End If


            Return _CalculateRemainingSeltelemntsNumber
        End Function

        Private Sub PaymentAdjestment()
            Dim dblTotalPrincipals As Double = 0
            For Each Pmt As PaymentSchedual In _PaymentSchedual
                dblTotalPrincipals += Pmt.dblPrincipalPayment
            Next
            Dim FirstPmt As PaymentSchedual = _PaymentSchedual(0)
            FirstPmt.dblPrincipalPayment += (_Balance - dblTotalPrincipals)
        End Sub
#End Region
    End Class
End Namespace


